package com.kx.smsplantform.service.impl;

import com.kx.smsplantform.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

/**
 * @Author Sun
 * @Date 2021/7/1 16:55
 */
@Service
public class CacheServiceImpl implements CacheService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //存储对象到hash结构
    @Override
    public void hMSet(String key, Map map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    //查询redis中客户的全部信息
    @Override
    public Map hGetAll(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    //根据key,filed查询value
    @Override
    public Object hGet(String key, String field) {
        return redisTemplate.opsForHash().get(key, field);
    }

    //存储数据到redis中string类型
    @Override
    public void saveCache(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public Set<Object> smembers(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    @Override
    public Object getBlack(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean sAdd(String key, String... set) {
        if("DIRTY_WORDS1:".equals(key)){
        Set<Object> members = redisTemplate.opsForSet().members(key);
        redisTemplate.opsForSet().remove(key, members.toArray(new Object[members.size()]));
        }
        redisTemplate.opsForSet().add(key, set);
        return true;
    }

    @Override
    public Set<Object> sIntersect(String key1, String key2) {
        return redisTemplate.opsForSet().intersect(key1, key2);

    }

    @Override
    public Set limit(String key, Long startTime, Long endTime) {
        return redisTemplate.opsForZSet().rangeByScore(key, startTime, endTime);
    }

    @Override
    public void zsave(String key, long time, String randomUUID) {
        redisTemplate.opsForZSet().add(key,randomUUID, time);
    }

    @Override
    public void saddMap(String key, Map map) {
        redisTemplate.opsForSet().add(key, map);
    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public Long hincr(String key, String field, long value) {
        return redisTemplate.opsForHash().increment(key,field,value);
    }

    @Override
    public Map hMGet(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public Set<String> hGetKeys(String pattern) {
        Set<String > keys = redisTemplate.keys(pattern);
        return keys;
    }
}
